//DOM渲染完成
$(function () {
    //初始化编辑器
    var editUm = UM.getEditor('editTacticsContent');
    editUm.ready(function () {
        editUm.execCommand('cleardoc');
    });
    //初始化行业资讯类型
    EasyAjax.ajax_Get_Json({
            url: "article/getArticleTypeList.json"
        },
        function (data) {
            var $editTacticsTypeTpl = $("#editTacticsTypeTpl").render(data.data),
                $searchTacticsTypeTpl = $("#searchTacticsTypeTpl").render(data.data);
            $(".editTacticsTypeTpl").append($editTacticsTypeTpl);
            $(".searchTacticsTypeTpl").append($searchTacticsTypeTpl);
            layui.use('form', function(){
                var form = layui.form();
            });
        });
    //获取父窗口nickName
    var nickName = window.parent.nickName;
    //行业资讯
    var $editTacticsTitle = $(".editTacticsTitle"),
        $editTacticsAuthor = $(".editTacticsAuthor"),
        $editTacticsTime = $(".editTacticsTime"),
        $editTacticsCover = $(".editTacticsCover"),
        $editTacticsType = $(".editTacticsType"),
        $searchTacticsDateStart = $(".searchTacticsDateStart"),
        $searchTacticsDateStop = $(".searchTacticsDateStop"),
        $searchTacticsType = $(".searchTacticsType"),
        $searchTacticsTitle = $(".searchTacticsTitle"),
        $searchTacticsAuthor = $(".searchTacticsAuthor"),
        $searchTacticsPublisher = $(".searchTacticsPublisher");
    //上传图片
    function upImgAjax(config) {
        EasyAjax.ajax_Upload_File({
                url: "file/articleUpload.json",
                data: config.fileData
            },
            function (data) {
                if(config.type){
                    config.obj.execCommand('inserthtml',"<img src='"+api_host+data.data+"' data-src='"+data.data+"' style='max-width: 100%;' alt='' />");
                }else {
                    config.obj.attr("src",api_host+data.data);
                    config.obj.attr("data-src",data.data);
                }
            });
    }
    //上传图片按钮
    $(".editUpImgFile").on("change",function(){
        var file=$(".editUpImgFile").get(0).files[0];
        var fileData=new FormData();
        fileData.append("upImg",file);
        $(".editUpImgFile").val("");
        upImgAjax({
            fileData:fileData,
            obj:$editTacticsCover,
            type:0
        });
    });
    //编辑器上传图片按钮
    $(".editUmUpImgFile").on("change",function(){
        var file=$(".editUmUpImgFile").get(0).files[0];
        var fileData=new FormData();
        fileData.append("upImg",file);
        $(".editUmUpImgFile").val("");
        upImgAjax({
            fileData:fileData,
            obj:editUm,
            type:1
        });
    });
    //插入老师按钮
    $(".editUmUpTeacher").on("click",function () {
        editUm.execCommand('inserthtml',"<div class='teacherBox' id='teacherBox'>老师信息显示区域</div>");
    });
    //分页
    function page(data) {
        $.each(data.data,function (index,ele) {
            var date=new Date(ele.createTime);
            this.createTime=formatDate(date);
        });
        var nums = 10;
        var render = function (curr) {
            var last = curr * nums - 1,
                tactics = new Array();
            last = last >= data.data.length ? (data.data.length - 1) : last;
            for (var i = (curr * nums - nums); i <= last; i++) {
                tactics.push(data.data[i]);
            }
            var $tactics = $("#tacticsTpl").render(tactics);
            return $tactics;
        };
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            laypage({
                cont: 'page',
                pages: Math.ceil(data.data.length / nums),
                skip: true,
                jump: function (obj, first) {
                    $(".showTacticsList").html(render(obj.curr));
                }
            });
        });
    }
    //初始化行业资讯
    function initTactics() {
        /*var searchTacticsDateStart = $searchTacticsDateStart.val(),
            searchTacticsDateStop = $searchTacticsDateStop.val(),
            searchTacticsType = $searchTacticsType.find(".layui-this").attr("lay-value"),
            searchTacticsTitle = $searchTacticsTitle.val(),
            searchTacticsAuthor = $searchTacticsAuthor.val(),
            searchTacticsPublisher = $searchTacticsPublisher.val(),
            baseValue = {
                searchTacticsDateStart: searchTacticsDateStart,
                searchTacticsDateStop: searchTacticsDateStop,
                searchTacticsType: searchTacticsType,
                searchTacticsTitle: searchTacticsTitle,
                searchTacticsAuthor: searchTacticsAuthor,
                searchTacticsPublisher: searchTacticsPublisher
            };*/
        var baseValue = {
            searchTacticsDateStart: "",
            searchTacticsDateStop: "",
            searchTacticsType: "",
            searchTacticsTitle: "",
            searchTacticsAuthor: "",
            searchTacticsPublisher: ""
        };
        EasyAjax.ajax_Post_Json({
                url: "article/getArticlesByExample.json",
                data: {baseValue: JSON.stringify(baseValue)}
            },
            function (data) {
                page(data);
            });
    }
    initTactics();
    //搜索行业资讯
    $(".searchTacticsBtn").on("click",function () {
        initTactics();
    });
    //修改按钮
    var editTacticsUid = "";
    $(document).on("click",".editTacticsBtn",function () {
        var $this = $(this);
        editTacticsUid = $this.parents("tr:first").attr("uid");
        EasyAjax.ajax_Get_Json({
                url: "article/queryArticle.json",
                data: {editTacticsUid:editTacticsUid}
            },
            function (data) {
                var showTacticsTitle = $this.parents("tr:first").find(".showTacticsTitle").html(),
                    showTacticsAuthor = $this.parents("tr:first").find(".showTacticsAuthor").html(),
                    showTacticsTime = $this.parents("tr:first").find(".showTacticsTime").html(),
                    showTacticsType = $this.parents("tr:first").find(".showTacticsType").attr("id"),
                    showTacticsCover = data.data.articleCover,
                    showTacticsContent = data.data.articleContent;
                $editTacticsTitle.val(showTacticsTitle);
                $editTacticsAuthor.val(showTacticsAuthor);
                $editTacticsTime.val(showTacticsTime);
                $editTacticsType.find("dd[lay-value="+showTacticsType+"]").click();
                $editTacticsCover.attr('src',api_host+showTacticsCover);
                $editTacticsCover.attr('data-src',showTacticsCover);
                editUm.setContent(showTacticsContent);
                layer.open({
                    type: 1,
                    title: "编辑文章",
                    shadeClose: true,
                    padding:10,
                    area: ['980px', '600px'],
                    content: $('.editTacticsTpl')
                });
            });
    });
    //添加按钮
    $(".addTacticsBtn").on("click",function () {
        editTacticsUid = "";
        $editTacticsTitle.val("");
        $editTacticsAuthor.val("");
        $editTacticsTime.val("");
        $editTacticsType.find("dd:first").click();
        $editTacticsCover.attr('src',"");
        $editTacticsCover.attr('data-src',"");
        editUm.execCommand('cleardoc');
        layer.open({
            type: 1,
            title: "新增文章",
            shadeClose: true,
            padding:10,
            area: ['980px', '600px'],
            content: $('.editTacticsTpl')
        });
    });
    //编辑行业资讯
    $(".savaEditTactics").on("click",function () {
        var editTacticsTitle = $editTacticsTitle.val(),
            editTacticsAuthor = $editTacticsAuthor.val(),
            editTacticsTime = $editTacticsTime.val(),
            editTacticsCover = $editTacticsCover.attr("data-src"),
            editTacticsType = $editTacticsType.find(".layui-this").attr("lay-value"),
            editTacticsContent = editUm.getContent(),
            $this = $(this);
        if(editTacticsUid){
            //修改行业资讯
            var baseValue = {editTacticsUid:editTacticsUid,editTacticsTitle:editTacticsTitle,editTacticsAuthor:editTacticsAuthor,editTacticsTime:editTacticsTime,editTacticsCover:editTacticsCover,editTacticsType:editTacticsType,editTacticsContent:editTacticsContent,publisher:nickName};
            EasyAjax.ajax_Post_Json({
                    url: "article/updateArticle.json",
                    data: {baseValue:JSON.stringify(baseValue)}
                },
                function (data) {
                    initTactics();
                    $this.parents(".layui-layer-page").find(".layui-layer-close").click();
                    layer.msg("修改成功！");
                });
        }else{
            //添加行业资讯
            var baseValue = {setTacticsTitle:editTacticsTitle,setTacticsAuthor:editTacticsAuthor,setTacticsTime:editTacticsTime,setTacticsCover:editTacticsCover,setTacticsType:editTacticsType,setTacticsContent:editTacticsContent,publisher:nickName};
            EasyAjax.ajax_Post_Json({
                    url: "article/addArticle.json",
                    data: {baseValue:JSON.stringify(baseValue)}
                },
                function (data) {
                    initTactics();
                    $this.parents(".layui-layer-page").find(".layui-layer-close").click();
                    layer.msg("添加成功！");
                });
        }
    });
    //删除行业资讯
    $(document).on("click",".delTacticsBtn",function () {
        editTacticsUid = $(this).parents("tr:first").attr("uid");
        layer.confirm('确认删除', {
            btn: ['确认','取消'],
            anim: 6
        }, function(){
            EasyAjax.ajax_Post_Json({
                    url: "article/delArticle.json",
                    data: {delTacticsUid:editTacticsUid}
                },
                function (data) {
                    initTactics();
                    layer.msg("删除成功！");
                });
        }, function(){

        });
    });
    //格式化日期
    function formatDate(date){
        var year=date.getFullYear();
        var month=date.getMonth()+1;
        var day=date.getDate();
        var hour=date.getHours();
        var minute=date.getMinutes();
        if(minute<10){
            minute="0"+minute;
        }
        var second=date.getSeconds();
        if(second<10){
            second="0"+second;
        }
        return year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second;
    }
});